<?php

class itemAction extends baseAction {

	public function index() {
		$id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : $this->error("404");
		$this->assign('items_id',$id);

		$items_mod = D('items');
		$items_cate_mod = D('items_cate');
		$item = $items_mod->find($id);
		$item['items_cate'] = $items_mod->relationGet("items_cate");
		$item['items_site'] = $items_mod->relationGet("items_site");
		$item['items_tags'] = $items_mod->relationGet("items_tags");
		$tag_str = '';
		foreach ($item['items_tags'] as $tag) {
			$tag_str .= $tag['name'] . ' ';
		}

		$this->seo['seo_title'] = !empty($item['seo_title']) ? $item['seo_title'] : $item['title'];
		$this->seo['seo_title'] = $this->seo['seo_title'] . ' - ' . $this->setting['site_name'];
		$this->seo['seo_keys'] = !empty($item['seo_keys']) ? $item['seo_keys'] : $tag_str;
		$item['seo_desc'] && $this->seo['seo_desc'] = $item['seo_desc'];

		//同大类商品
		$siblings_cate_group = $items_cate_mod->where("pid=" . $item['items_cate']['pid'] . " AND is_hots=1")->limit('0,4')->order("status DESC,ordid DESC")->select();
		foreach ($siblings_cate_group as $key => $val) {
			$siblings_cate_group[$key]['items'] = $this->get_group_items($val['id']);
		}
		$items_list = $items_mod->relation('items_site')->where('cid=' . $item['cid'])->limit('0,20')->select(); //同类商品
		$this_cate_group = $this->get_group_items($item['cid']); //所在分类展示
		$source_group = $this->get_group_items_bysource($item['sid']); //相同来源展示
		$items_mod->where('id=' . $id)->setInc('hits'); //浏览次

		$this->assign('seo', $this->seo);
		$this->assign('item', $item);
		$this->assign('items_list', $items_list);
		$this->assign('siblings_cate_group', $siblings_cate_group);
		$this->assign('this_cate_group', $this_cate_group);
		$this->assign('source_group', $source_group);
		$res=$this->items_comments_mod->get_items_comments($id);
		$this->assign('comments',$res);
		
		if($this->isAjax()){
            $this->ajaxReturn(array('list'=>$this->fetch('comments_list'),'count'=>$res['count']));
		}
		$this->display();
	}

	public function like() {
		$items_cate_mod=D('items_cate');

		if(!$this->check_login()){
			$this->ajaxReturn('not_login');
		}
		$id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';

		if(check_favorite('like_list', $id)){
			$this->ajaxReturn('yet_exist');
		}

		$data = 0;
		if ($id) {
			$items_mod = D('items');
			$data = $items_mod->where('id=' . $id)->setInc('likes');
			$items_mod->where('id=' . $id)->setField('status',1);
				
			$items=$items_mod->where('id=' . $id)->find();
			$items_cate_mod->where('id='.$items['cid'])->setInc('item_likes');
		}
		$this->ajaxReturn($data);
	}
	function tao(){
	   $id=intval($_REQUEST['id']);
	   $res=$this->items_mod->where('id='.$id)->find();
	   if($res){
	       redirect($res['url']);
	   }
	}
	function img(){
	   $id=intval($_REQUEST['id']);
	   $type=$_REQUEST['type'];
	   $res=$this->items_mod->where('id='.$id)->find();
       if($res){
           header("content-type:image/jpg");
           print_r(file_get_contents($res[$type]));
       }
	}
}